<?php
/**
 * Users controller class
 *
 */

class AppController extends Admin_Controller
{
    //private $table_tags;

	public function init()
    {
    	parent::init();
    	$this->_table = new Admin_Table_App();
    	$this->setActiveCat(980);

    	//$this->_table_field = 'id_auction'; // for table objects
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	$this->page_type = 'list';

    	$reset = $this->_request->getParam('reset', '');
    	$popular = $this->_request->getParam('popular', 0);

    	$form = new Llib_Form($this->_request);
    	$data = array();

    	if ($this->_request->isPost())
    	{
	    	$data['is_active'] = $this->_request->getParam('is_active', -1);
	    	$data['is_top'] = $this->_request->getParam('is_top', -1);
	    	//$data['id_cat'] = $this->_request->getParam('id_cat', -1);
	    	//$data['id_lang'] = $this->_request->getParam('id_lang', -1);
	    	$data['s'] = $this->_request->getParam('s', '');
    	}

        $params = $this->SessionVars($data, array('is_active', 'is_top', 's'), $reset);

    	$lclm = new Llib_List($this->_request, $this->_sess);
    	$lclm->AddParams(array('checkboxes' => true));
    	$lclm->AddColumn('Кадастровий номер', 'cadnum', array('width' => '20%', 'sort' => 1));
    	$lclm->AddColumn('ФІО', 'name', array('width' => '40%', 'sort' => 1));
    	$lclm->AddColumn('e-mail', 'email', array('width' => '20%', 'sort' => 1));
    	$lclm->AddColumn('Дата формування звернення', 'add_date', array('width' => '40%', 'sort' => 1));

    	//$lclm->AddFunc(array('title' => 'Головна', 'name' => 'top', 'field' => 'is_top'));
    	//$lclm->AddFunc(array('title' => 'Опубліковано', 'name' => 'public', 'field' => 'is_public'));
    	$lclm->AddFunc();

    	$lclm->AddData($this->_table->GetDataList($lclm->page, $lclm->s, $lclm->pager_count, $params));

        $data['is_active'] = $form->GetActiveList(@$params['is_active']);
        //$data['is_top'] = $form->GetTopList(@$params['is_top']);
        //$data['id_cat'] = $this->table_categories->GetCatList(array($params['id_cat']), array('id_type' => 3));
        //$data['id_lang'] = $this->_table->GetLangList(@$params['id_lang']);
        $data['s'] = $params['s'];

        $form->AddData($data);

        $this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('List', $lclm->GetList());
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    public function editAction()
    {
    	$id = $this->_getParam('id');
    	$saveexit = $this->_getParam('saveexit');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$data = $this->_table->GetDataRow($id);

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost())
        {
			$d = $form->GetPostedData(array('tags'));
			if($this->_table->update($d, $id, $this->_user->id))
			{
			   if(is_null($saveexit))  $form->SetSuccess();
			   else $this->_redirect('/'.$this->controller);
			}
        	else $form->SetError($this->_table->GetError());
        }

        $form->AddData($data);

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->xmlWriter->writeAttribute('id', $id);
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

}
